#pragma once


#include <opencv2/opencv.hpp> 
using namespace cv;


/// <summary>
/// 
/// </summary>
/// <param name="image"></param>
void face_detection_demo()
{
	std::string root_dir = "Z:/_PROJECT_/OPENCV/face_detector/";
	dnn::Net net
		= dnn::readNetFromTensorflow(
			root_dir + "opencv_face_detector_uint8.pb",
			root_dir + "opencv_face_detector.pbtxt");

	VideoCapture capture("Z:/_PROJECT_/OPENCV/Megamind.avi");
	Mat frame;
	while (true)
	{
		capture.read(frame);
		//若无视频讯号则退出
		if (frame.empty()) { break; }
		//DNN张量构建   1.0图像保持0-255色彩空间  0.0384为单一化   scalar为当前DNN模型均值文件中来    是否交换 和 剪切  false 
		Mat blob = dnn::blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123), false, false);
		//  blob  N个数  C输入通道数   H高度   W宽度
		net.setInput(blob); //NCHW
		//开始根据模型推理
		Mat probs = net.forward(); //   

		//TODO:dosomthing
		//拿到解析结果
		Mat detectionMat(probs.size[2], probs.size[3], CV_32F, probs.ptr<float>());
		//解析
		for (int i = 0; i < detectionMat.rows; i++)
		{
			float confidence = detectionMat.at<float>(i, 2);
			if (confidence > 0.5)
			{
				int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
				int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
				int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
				int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
				Rect box(x1, y1, x2 - x1, y2 - y1);
				rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
			}
		}
		imshow("face-detec", frame);

		int c = waitKey(10);
		if (c == 27) { break; } 
	}
	capture.release();
	frame.release();
}



/// <summary>
/// 
/// </summary>
/// <param name="image"></param>
void face_detection_demo(std::string videoPath , std::string netDir )
{ 
	dnn::Net net
		= dnn::readNetFromTensorflow(
			netDir + "opencv_face_detector_uint8.pb",
			netDir + "opencv_face_detector.pbtxt");

	VideoCapture capture(videoPath);
	Mat frame;
	while (true)
	{
		capture.read(frame);
		//若无视频讯号则退出
		if (frame.empty()) { break; }
		//DNN张量构建   1.0图像保持0-255色彩空间  0.0384为单一化   scalar为当前DNN模型均值文件中来    是否交换 和 剪切  false 
		Mat blob = dnn::blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123), false, false);
		//  blob  N个数  C输入通道数   H高度   W宽度
		net.setInput(blob); //NCHW
		//开始根据模型推理
		Mat probs = net.forward(); //   

		//TODO:dosomthing
		//拿到解析结果
		Mat detectionMat(probs.size[2], probs.size[3], CV_32F, probs.ptr<float>());
		//解析
		for (int i = 0; i < detectionMat.rows; i++)
		{
			float confidence = detectionMat.at<float>(i, 2);
			if (confidence > 0.5)
			{
				int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
				int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
				int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
				int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
				Rect box(x1, y1, x2 - x1, y2 - y1);
				rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
			}
		}
		imshow("face-detec", frame);

		int c = waitKey(10);
		if (c == 27) { break; }
	}
	capture.release();
	frame.release();
}